package za.um.td.util.texture;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import za.um.td.entity.level.Terrain;
import za.um.td.entity.level.Tile;
import za.um.td.state.BasicGameStateSingleton;
import za.um.td.util.XMLUtil;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.jme.image.Texture;
import com.jme.util.TextureKey;
import com.jme.util.TextureManager;

public class TextureSetManager {
	private static final Logger LOG = Logger.getLogger(TextureSetManager.class.getName());
	
	private static HashMap<String,TextureSet> textureset;
	
	private static TextureSetManager textureSetManager = null;
	
	public final static String NODE_TEXTURESETS = "texturesets";
	
	public final static String TextureSetXML = "za/um/td/data/texture/sets/texturesets.xml";
	
	public static TextureSetManager getInstance()
	{
		if(textureSetManager==null)
			textureSetManager = new TextureSetManager();
		return textureSetManager;
	}
	
	public TextureSetManager() {
		URL xmlUrl = TextureSetManager.class.getClassLoader().getResource(TextureSetXML);
		init(xmlUrl);
	}	

	public void init(URL xmlUrl)
	{
		textureset = new HashMap<String,TextureSet>();
		load(xmlUrl);
	}
	
	public void load(URL xmlUrl)
	{
		try {
			Node texturesetsNode = XMLUtil.loadDocument(xmlUrl.openStream(), TextureSetManager.NODE_TEXTURESETS);

			NodeList textureSetsNodeList = texturesetsNode.getChildNodes();
			
			for(int i=0; i <textureSetsNodeList.getLength(); i++)
			{
				Node textureSetNode = textureSetsNodeList.item(i);
				
				if(textureSetNode.getNodeName().equals(TextureSet.NODE_TEXTURESET))
				{
					String id = XMLUtil.getAttribute(textureSetNode,TextureSet.ATRI_NAME);
					TextureSet texSet = new TextureSet(id);
					
					textureset.put(id, texSet);
					
					NodeList textureteNodeList = textureSetNode.getChildNodes();
					int order = 0;
					for(int j=0; j <textureteNodeList.getLength(); j++)
					{
						Node textureteNode = textureteNodeList.item(j);
						if(textureteNode.getNodeName().equals(Texturete.NODE_TEXTURE))
						{
							String idTexturete = XMLUtil.getAttribute(textureteNode, Texturete.ATRI_NAME);
							
							LOG.log(Level.FINEST,"idTexturete="+idTexturete);
							int scale = XMLUtil.getIntAttribute(textureteNode, Texturete.ATRI_SCALE);
						
							String defuse = XMLUtil.getAttribute(textureteNode, Texturete.ATRI_DEFUSE);
							String normal = XMLUtil.getAttribute(textureteNode, Texturete.ATRI_NORMAL);
							String specular = XMLUtil.getAttribute(textureteNode, Texturete.ATRI_SPECULAR);
							
							Texturete tex = new Texturete(idTexturete);
							
							tex.setScale(scale);
							tex.setOrder(order);
							if(idTexturete != null)
								tex.setTexture(TextureSetManager.loadTexture(idTexturete));
							LOG.log(Level.FINEST,texSet.getId()+"["+j+"]"+tex.getTexture().getImageLocation());
							if(defuse != null)
								tex.setTextureDefuse(TextureSetManager.loadTexture(defuse));
							
							if(normal != null)
								tex.setTextureNormal(TextureSetManager.loadTexture(normal));
							
							if(specular != null)
								tex.setTextureSpecular(TextureSetManager.loadTexture(specular));
							
							texSet.addTexturete(tex);
							
							order++;
						}
					}
				}
				
			}
			
		} catch (IOException e) {
			LOG.log(Level.SEVERE,e.getMessage()+"\t"+e.getStackTrace());
		}
		
	}
	
	public static Texture loadTexture(String textureURL)
	{
		Texture t0 = null;
		String path = "";
		
		for(int i=-1; i < BasicGameStateSingleton.texturePaths.length; i++)
		{
			if(i>-1)
				path = BasicGameStateSingleton.texturePaths[i];
			try
			{
				LOG.log(Level.FINEST,"["+i+"]path="+path+textureURL);
				t0 = TextureManager.loadTexture(Terrain.class
	                .getClassLoader().getResource(path+textureURL),
	                com.jme.image.Texture.MinificationFilter.Trilinear,
	                com.jme.image.Texture.MagnificationFilter.Bilinear);
				
				if(t0!=null)
				{
					if(!t0.getImageLocation().endsWith("notloaded.png"))
					{
						LOG.log(Level.FINEST,"FOUND!="+t0.getImageLocation());
						return t0;
					}
				}
			}
			catch(NullPointerException e)
			{
				LOG.log(Level.SEVERE,e.getMessage()+"\t"+e.getStackTrace());
			}
			
		}
		return null;
	}
	
	public boolean hasTextureSet(String id)
	{
		return textureset.containsKey(id);		
	}
	
	public TextureSet get(String id)
	{
		return textureset.get(id);
	}
	
	/**
	 * @param args
	 */
	/*
	public static void main(String[] args) 
	{
		TextureSetManager tm = TextureSetManager.getInstance();
        
        TextureSet texSet = tm.get("bb0x material set");
        
        int size = texSet.size();
        
        List lst = texSet.getTextureteList();
        
        for(int i=0; i < lst.size(); i++)
        {
        	System.out.println("["+i+"]");
        	Texturete tex = (Texturete)lst.get(i);
        	System.out.println("tex="+tex.getId() +"\t"+tex.getOrder());
        	Texture texture = tex.getTexture();
        	
        	System.out.println(texture.getImageLocation());
        }
	}*/

}
